package com.baidu.service.impl;

import com.baidu.mapper.TbCategoryMapper;
import com.baidu.pojo.TbCategory;
import com.baidu.utils.ResultResponse;
import com.baidu.vo.TbGoodsAuditVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbGoodsAudit;
import com.baidu.service.TbGoodsAuditService;
import com.baidu.mapper.TbGoodsAuditMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbGoodsAuditServiceImpl extends ServiceImpl<TbGoodsAuditMapper, TbGoodsAudit> implements TbGoodsAuditService{

    @Autowired
    private TbGoodsAuditMapper tbGoodsAuditMapper;


    @Autowired
    private TbCategoryMapper categoryMapper;

    /**
     * 分页模糊查询
     * @param page
     * @param size
     * @param name
     * @return
     */
    @Override
    public ResultResponse findList(Integer page, Integer size, String name) {
        //开启分页
        Page<TbGoodsAuditVo> page1 = new Page<>(page,size);
        Page<TbGoodsAuditVo> list = this.tbGoodsAuditMapper.findList(page1,name);
        List<TbGoodsAuditVo> records = list.getRecords();
        for (int i = 0; i < records.size(); i++) {
            QueryWrapper<TbCategory> id = new QueryWrapper<TbCategory>().eq("id", records.get(i).getCid());
            TbCategory three = categoryMapper.selectOne(id);

            TbCategory two = categoryMapper.selectOne(new QueryWrapper<TbCategory>().lambda().eq(TbCategory::getId, three.getParentId()).last("limit 1"));

            TbCategory one = categoryMapper.selectOne(new QueryWrapper<TbCategory>().lambda().eq(TbCategory::getId, two.getParentId()).last("limit 1"));

            records.get(i).setTid(Integer.parseInt(two.getId().toString()));

            records.get(i).setOid(Integer.parseInt(one.getId().toString()));

            records.get(i).setCname(three.getName());
            records.get(i).setTwoName(two.getName());
            records.get(i).setOneName(one.getName());
        }
        return ResultResponse.SUCCESS(list);
    }

    /**
     * 批删
     * @param ids
     * @return
     */
    @Override
    public ResultResponse deleteIds(Long[] ids) {
        for (Long id : ids) {
            this.tbGoodsAuditMapper.deleteById(id);
        }
        return ResultResponse.SUCCESS();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public ResultResponse findById(Long id) {
        TbGoodsAudit tbGoodsAudit = this.tbGoodsAuditMapper.selectById(id);
        QueryWrapper<TbCategory> ids = new QueryWrapper<TbCategory>().eq("id", tbGoodsAudit.getCid());
        //三级联动查询规格
        TbCategory three = categoryMapper.selectOne(ids);

        TbCategory two = categoryMapper.selectOne(new QueryWrapper<TbCategory>().lambda().eq(TbCategory::getId, three.getParentId()).last("limit 1"));

        TbCategory one = categoryMapper.selectOne(new QueryWrapper<TbCategory>().lambda().eq(TbCategory::getId, two.getParentId()).last("limit 1"));

        tbGoodsAudit.setCname(three.getName());
        tbGoodsAudit.setTwoName(two.getName());
        tbGoodsAudit.setOneName(one.getName());
        return ResultResponse.SUCCESS(tbGoodsAudit);
    }


    /**
     * 拒绝审核
     * @param ids
     * @return
     */
    @Override
    public ResultResponse auditReject(Long[] ids) {
        for (Long id : ids) {
            System.err.println(id);
            this.tbGoodsAuditMapper.auditReject(id);
        }
        return ResultResponse.SUCCESS();
    }

    /**
     * 审核通过
     * @param ids
     * @return
     */
    @Override
    public ResultResponse auditPass(Long[] ids) {
        for (Long id : ids) {
            System.err.println(id);
            this.tbGoodsAuditMapper.auditPass(id);
        }
        return ResultResponse.SUCCESS();
    }
}




